/*
 * LoadResource.java
 *
 * Created on 16 de julio de 2006, 0:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package resource;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Hashtable;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

/**
 *
 * @author Administrador
 */
public class LoadResource {
    
    private static final boolean bActivarSonidos=false;
    private static final int	EXTERNAL_BUFFER_SIZE = 128000;    
    
    private static Hashtable<String, Clip> sonidos=new Hashtable<String, Clip>();
    
    private static LoadResource lr=new LoadResource();
    public static LoadResource getDefault(){
        return lr;
    }
    
    public Image getCampo() throws Exception{
        URL url=getClass().getResource("Campo.jpg");
        return Toolkit.getDefaultToolkit().getImage(url);
    }
    
    public Image getIcono() throws Exception{
        URL url=getClass().getResource("Icono.gif");
        return Toolkit.getDefaultToolkit().getImage(url);
    }
    
    public Image getLogo() throws Exception{
        URL url=getClass().getResource("Logo.jpg");
        return Toolkit.getDefaultToolkit().getImage(url);
    }
    
    public Image getChampions() throws Exception{
        URL url=getClass().getResource("Champions.jpg");
        return Toolkit.getDefaultToolkit().getImage(url);
    }    

    private void loadSound(String sNombre) throws Exception{
        URL url=getClass().getResource(sNombre);
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
        AudioFormat	format = audioInputStream.getFormat();
        DataLine.Info	info = new DataLine.Info(Clip.class, format);
        Clip m_clip = (Clip) AudioSystem.getLine(info);
        m_clip.open(audioInputStream);
        sonidos.put(sNombre, m_clip);
    }
    
    public void playSound(String sNombre) {
        try
	{
            if (!sonidos.containsKey(sNombre))
                loadSound(sNombre);
            if (sonidos.containsKey(sNombre)){
                Clip m_clip=sonidos.get(sNombre);
                m_clip.setFramePosition(0);
                m_clip.start();
            }
	}
	catch (Exception e)
	{
		//e.printStackTrace();
	}
    }
    
    public void playSoundKit() {
        try{
            if (bActivarSonidos)return;
            playSound("kit.wav");
        }catch(Exception e){}
    }
    
    public void playSoundSilbato() {
        try{
            if (bActivarSonidos)return;
            playSound("silbato.wav");
        }catch(Exception e){}
    }
    
    public void playSoundRayos() {
        try{
            if (bActivarSonidos)return;
            playSound("rayos.wav");
        }catch(Exception e){}
    }    
    
}
